package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.Class;
import com.zyg.pojo.User;
import com.zyg.service.ClassService;
import com.zyg.util.LayData;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/class")
public class ClassController {


    @Resource
    private ClassService classService;

    @RequestMapping("/tolist")
    public  String tolist(){
        return "class/list";
    }


    @RequestMapping("/list")
    @ResponseBody
    public String list(Class clas, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        // 查询数据
        LayData layData = new LayData();
        QueryWrapper<Class> qw = new QueryWrapper<>();
        if (clas.getClassName() != null && !"".equals(clas.getClassName())) {
            qw.like("class_name", clas.getClassName());
        }
        if (clas.getGradeId() != null) {
            qw.eq("grade_id", clas.getGradeId());
        }
        long totalCount = classService.count(qw);
        layData.setCount((int) totalCount);
        Page<Class> page1 = new Page<>(page, limit);
        layData.setData(classService.list(page1, qw));
        layData.setCode(0);
        layData.setMsg("查询成功");
        // 数据返回
        String data = JSON.toJSONString(layData);
        return data;
    }

    @RequestMapping("/toadd")
    public String toAdd(){
        return "class/add";
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(Class clas){
        boolean result = classService.save(clas);
        if (result) {
            return JSON.toJSONString(new LayData(0, "添加成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "添加失败"));
        }
    }

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id, Model model){
        Class cla = classService.getById(id);
        model.addAttribute("class", cla);
        return "class/update";
    }

    @RequestMapping("/toinfo/{id}")
    public String toInfo(@PathVariable("id") int id, Model model){
        Class clas = classService.getById(id);
        model.addAttribute("class", clas);
        return "class/info";
    }

    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(Class clas){
        boolean result = classService.updateById(clas);
        if (result) {
            return JSON.toJSONString(new LayData(0, "更新成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "更新失败"));
        }
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String deleteClass(@PathVariable("id") int id){
        boolean result = classService.removeById(id);
        if (result) {
            return JSON.toJSONString(new LayData(0, "删除成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "删除失败"));
        }
    }


}
